<?php
require_once("./define/define_table.php");
require_once("./class/Individu.php");
require_once("./class/Evenement.php");

define("ADD_PARTICIPANT_FORM", "Formulaire d'ajout d'un participant");

class Participant extends Individu{

    private $id_evenement;

    public function Participant(){
    }

    public function getIdEvenement(){
        return $this->id_evenement;
    }

    public function setIdEvenement($id){
        $this->id_evenement = $id;
    }

    public function getAllParticipant($id_evenement = null){
        global $db;
        try{
            if($id_evenement == null){
                $sql = "SELECT id_individu, nom, prenom, id_evenement FROM ".PARTICIPANT_TABLE."
					JOIN ".INDIVIDU_TABLE." USING(id_individu)";
                $sth = $db->prepare($sql);
            }
            else{
                $sql = "SELECT id_individu, nom, prenom, id_evenement FROM ".PARTICIPANT_TABLE."
												JOIN ".INDIVIDU_TABLE." USING(id_individu)
												WHERE id_evenement = :id_evenement";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_evenement", $id_evenement, PDO::PARAM_INT);
            }
            $sth->execute();
            PrintQuery::queryPrint($sth);
            return $sth->fetchAll(PDO::FETCH_CLASS, "Participant");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function getParticipant($id){
        global $db;
        try{
            $sql = "SELECT id_individu, nom, prenom, id_evenement  FROM ".PARTICIPANT_TABLE."
           			JOIN ".INDIVIDU_TABLE." USING(id_individu)
            		WHERE id_individu = :id_individu";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_individu", $id, PDO::PARAM_INT);
            $sth->execute();
            return $sth->fetchObject("Participant");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }

    public function addOrModify(){
        global $db;
        $sql = "INSERT INTO ".PARTICIPANT_TABLE." VALUES(:id_individu, :id_evenement)";
        $sth = $db->prepare($sql);
        $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
        $sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
        $sth->execute();
        PrintQuery::queryPrint($sth);
    }

    public static function getAddForm(){
        $html = ADD_PARTICIPANT_FORM .
				'<form action="?module=participant&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_INDIVIDU_.'</td>' .
				'			<td>'.Individu::generateSelect().'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_EVENEMENT_.'</td>' .
				'			<td>'.Evenement::generateSelect().'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
        return $html;
    }

    public function getModifyForm(){
        $html = MODIFY_PARTICIPANT_FORM .
				'<form action="?module=participant&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_INDIVIDU_.'</td>' .
				'			<td><input readonly type="text" name="id_individu" value="'.$this->id_individu.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_EVENEMENT_.'</td>' .
				'			<td><input type="text" name="id_evenement" value="'.$this->id_evenement.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
        return $html;
    }

    public function delete(){
        global $db;
        $sql = "DELETE FROM ".PARTICIPANT_TABLE." WHERE id_individu = :id_individu AND id_evenement = :id_evenement";
        $sth = $db->prepare($sql);
        $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
        $sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
        $sth->execute();
        PrintQuery::queryPrint($sth);
    }

    public function generateSelect($id_evenement){
        $array = self::getAllParticipant($id_evenement);
        $html = '<select name="id_individu">';
        foreach($array as $i){
            $html .= '<option value="'.$i->getIdIndividu().'">'.$i->getNom().' '.$i->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }

}

?>
